/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.xeerkat.xmpp;

import org.infoset.xml.Child;
import org.infoset.xml.Element;
import org.infoset.xml.Item;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.Parent;

/**
 *
 * @author alex
 */
public class StanzaDestination implements ItemDestination {
   protected int level;
   protected Element top;
   protected Parent current;
   public StanzaDestination() {
      this.level = 0;
      this.top = null;
      this.current = null;
   }
   public void send(Item item) {
      switch (item.getType()) {
         case ElementItem:
            level++;
            if (current==null) {
               top = (Element)item;
               current = top;
            } else {
               current.add((Child)item);
               current = (Element)item;
            }
            break;
         case ElementEndItem:
            level--;
            current = current.getParent();
            if (level==0) {
               onStanza();
            }
            break;
         case CharactersItem:
         case CommentItem:
         case ProcessingInstructionItem:
            if (current!=null) {
               current.add((Child)item);
            }
      }
   }

   public Element getStanza() {
      return top;
   }

   public void onStanza() {
   }

}
